candidates = [2, 3, 6, 7], target = 7

var combinationSum = function (candidates, target) {
    let result = []
    let zuhe = []
    let sum = 0
    let len = candidates.length

    function dfs(index) {

        if (sum > target) {
            return;
        }
        if (sum === target) {
            result.push(zuhe.slice())
            return;
        }

        for (let i = index; i < len; i++) {
            zuhe.push(candidates[i])
            sum += candidates[i]
            dfs(i)
            sum -= candidates[i]
            zuhe.pop()
        }
    }

    dfs(0)
    return result
};

console.log(combinationSum(candidates, target));
